//
//  ZLPublicNoticeDetailViewController.m
//  ZLExamination
//
//  Created by Lei_Zhong_Xun on 2017/8/21.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "ZLPublicNoticeDetailViewController.h"
#import "ZLPublicNoticeModel.h"

@interface ZLPublicNoticeDetailViewController ()
/** 详情model*/
@property (nonatomic,strong) ZLPublicNoticeModel* noticeModel;
@end

@implementation ZLPublicNoticeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"公告详情";
    self.view.backgroundColor = [UIColor whiteColor];
    [self listLoadData];
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.timeLabel];
    [self.view addSubview:self.contentLabel];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(10);
        make.right.equalTo(self.view.mas_right).offset(- 15);
        make.top.equalTo(self.view.mas_top).offset(15);
    }];
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(10);
        make.right.equalTo(self.view.mas_right).offset(- 10);
        make.top.equalTo(_titleLabel.mas_bottom).offset(8);
        make.height.equalTo(@13);
    }];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(10);
        make.right.equalTo(self.view.mas_right).offset(- 10);
        make.top.equalTo(_timeLabel.mas_bottom).offset(16);
    }];
}

#pragma mark - 数据加载
- (void)listLoadData
{
    [[YCRequestDataService sharedManager] Post:@"notice/notice/info" parameters:@{@"id" : _n_id} success:^(id response) {
        if (response[@"error_code"]) {
            NSLog(@"%@",response[@"error_msg"]);
        } else {
            NSDictionary *dic = response[@"response_data"][@"lists"];
                _noticeModel = [[ZLPublicNoticeModel alloc] initWithDataDic:dic];
            _titleLabel.text = _noticeModel.title;
            NSString *timeStr = [self timeWithTimeIntervalString:_noticeModel.add_time];
            _timeLabel.text = timeStr;
            NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[_noticeModel.content dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];

            _contentLabel.attributedText = attrStr;
        }
    }];
}

//转换时间
- (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"beijing"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:[timeString doubleValue]/ 1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}

#pragma mark - 布局
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

}


#pragma mark - lazy load
- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = kCX_FONT(16);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = kRGB_COLOR(51, 51, 51, 1.0);
//        _titleLabel.text = @"全国依法治国前景退稿司考温度";
        _titleLabel.numberOfLines = 0;
        [_titleLabel sizeToFit];
    }
    return _titleLabel;
}
- (UILabel *)timeLabel
{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = kCX_FONT(13);
        _timeLabel.textAlignment = NSTextAlignmentCenter;
        _timeLabel.textColor = kRGB_COLOR(153, 153, 153, 1.0);
//        _timeLabel.text = @"2017-08-09";
    }
    return _timeLabel;
}
- (UILabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
//        _contentLabel.backgroundColor = [UIColor clearColor];
//        _contentLabel.text = @"全国依法治国前景退稿司考温度,全国依法治国前景退稿司考温度全国依法治国前景退稿司考温度全国依法治国前景退稿司考温度,全国依法治国前景退稿司考温度.\n全国依法治国前景退稿司考温度,全国依法治国前景退稿司,maximumLabelSize";
//        _contentLabel.lineBreakMode = NSLineBreakByTruncatingTail;
//        CGSize maximumLabelSize = CGSizeMake(100, 9999);//labelsize的最大值
        //关键语句
//        CGSize expectSize = [_contentLabel sizeThatFits:maximumLabelSize];
        //别忘了把frame给回label，如果用xib加了约束的话可以只改一个约束的值
//        _contentLabel.frame = CGRectMake(0, 0, expectSize.width, expectSize.height);
        _contentLabel.font = kCX_FONT(13);
        _contentLabel.textColor = kRGB_COLOR(51, 51, 51, 1.0);
        _contentLabel.numberOfLines = 0;
        [_contentLabel sizeToFit];

    }
    return _contentLabel;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
